package net.lahuobao.finance.controller.registerManagement;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.Logger;
import org.jeecgframework.core.common.controller.BaseController;
import org.jeecgframework.core.common.model.json.AjaxJson;
import org.jeecgframework.core.common.model.json.DataGrid;
import org.jeecgframework.tag.core.easyui.TagUtil;
import org.jeecgframework.web.system.service.SystemService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import net.lahuobao.finance.dao.UserMapper;
import net.lahuobao.finance.entity.depart.EUserState;
import net.lahuobao.finance.entity.depart.User;
import net.lahuobao.finance.exception.RegionCodeException;
import net.lahuobao.finance.exception.ValidationException;
import net.lahuobao.finance.service.userManagement.IUserService;
import net.lahuobao.util.AliAddress;
import net.lahuobao.util.IDVerifyUtil;

/**   
 * @Title: Controller
 * @Description: 资金明细账
 * @author zhangdaihao
 * @date 2016-08-24 18:49:17
 * @version V1.0   
 *
 */
@Controller
@RequestMapping("/userManagerController")
public class UserManagerController extends BaseController {
	/**
	 * Logger for this class
	 */
	private static final Logger logger = Logger.getLogger(UserManagerController.class);

	@Autowired
	private SystemService systemService;
	@Autowired
	private UserMapper userDao;
	@Autowired
	private IUserService userManagementService;




	/**
	 * 资金明细 页面跳转
	 * 
	 * @return
	 */
	@RequestMapping(params = "list")
	public ModelAndView list(HttpServletRequest request) {
		return new ModelAndView("net/lahuobao/finance/registerManagement/userList");
	}


	/**
	 * easyui AJAX请求数据
	 * 
	 * @param request
	 * @param response
	 * @param dataGrid
	 * @param user
	 */

	@RequestMapping(params = "datagrid")
	public void datagrid(HttpServletRequest request, HttpServletResponse response, DataGrid dataGrid) {
		Map paramMap = new HashMap();//sql 参数
	
		paramMap.put("fullname",request.getParameter("fullname"));
		paramMap.put("userName",request.getParameter("userName"));

		paramMap.put("validateState",request.getParameter("validateSate"));
		

		paramMap.put("state",request.getParameter("state"));
		
		

		List<Map> list=userDao.selectAllOwnerUser(systemService.getPageBounds(),paramMap);
	
        TagUtil.datagrid(response, pageDateGrid(list, dataGrid));

	}

//	@RequestMapping(params="toUserDetail")
//	@ResponseBody
//	public Map toUserDetail(Integer userId){
//		Map map = userDao.selectMapByUserId(userId);
//		return map;
//	}
	 //禁用用户
    @RequestMapping(value="/blockuser")
	@ResponseBody
    public AjaxJson blockUser(HttpServletRequest request) throws RegionCodeException, ValidationException {
		AjaxJson j = new AjaxJson();
    	Integer userId = Integer.parseInt(request.getParameter("userId").toString());
        User user = userManagementService.selectUserById(userId);
        user.setState(EUserState.DISABLED.getValue());
        userManagementService.updateUser(user);
        
        j.setMsg("禁用操作完成");
        return j;
		//return new ModelAndView("net/lahuobao/finance/registerManagement/userList");
    }
    @RequestMapping(value = "/unblockuser")
	@ResponseBody
    public AjaxJson unblockUser(HttpServletRequest request) throws RegionCodeException, ValidationException {
    	AjaxJson j = new AjaxJson();
    	Integer userId = Integer.parseInt(request.getParameter("userId").toString());
        User user = userManagementService.selectUserById(userId);
        user.setState(EUserState.ENABLED.getValue());
        userManagementService.updateUser(user);

        j.setMsg("解禁操作完成");
        return j;   
        }

  //删除用户
    @RequestMapping(value = "/deleteuser")
	@ResponseBody
    public AjaxJson deleteUser(HttpServletRequest request) throws RegionCodeException, ValidationException {
    	AjaxJson j = new AjaxJson();

    	Integer userId = Integer.parseInt(request.getParameter("userId").toString());
        User user = userManagementService.selectUserById(userId);
        user.setState(EUserState.DELETED.getValue());
        userManagementService.updateUser(user);
        j.setMsg("删除操作完成");
        return j;   
    }
    //同意用户
    @RequestMapping(value = "/approveuser")
    @ResponseBody
    public AjaxJson approveUser(HttpServletRequest request, Integer userId) {
    	AjaxJson j = new AjaxJson();

    	userManagementService.approveUser(userId);

    	 j.setMsg("通过操作完成");
         return j;
    }
    //禁用用户
    @RequestMapping(value = "/denyuser")
    @ResponseBody
    public AjaxJson denyUser(HttpServletRequest request, Integer userId) throws RegionCodeException, ValidationException {
    	AjaxJson j = new AjaxJson();

    	userManagementService.denyUser(userId);

    	 j.setMsg("拒绝操作完成");
         return j;    
        }
    /**
     * 国政通接口验证姓名身份证号是否匹配
     *
     * @param userId
     * @return
     * @throws Exception
     */
    @RequestMapping(value = "/id-check")
    @ResponseBody
    public Boolean idCheck(Integer userId) throws Exception {
        boolean result;
        User user = userManagementService.selectUserById(userId);
        result = IDVerifyUtil.querySingle(user.getFullName(), user.getIdcard());
        return result;
    }
}
